<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.zip.*"%>
<%@ page import="org.jbpm.*"%>
<%@ page import="org.jbpm.graph.def.*"%>
<%@ page import="com.gzgi.workflow.jbpm.mgmt.service.*"%>
<%@ page import="com.gzgi.workflow.jbpm.model.*"%>
<%@ page import="com.gzgi.workflow.jbpm.context.*"%>
<%@ page import="com.gzgi.framework.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.config.*"%>
<%!
    private static JbpmConfiguration jbpmConfiguration = JbpmConfiguration
			.getInstance();
    public  void installSystemJbpmProcessDefinitions() {
		JbpmContext jbpmContext = null;
		DeployManager deployManager = null;
		try {
			deployManager = (DeployManager) ContextFactory
					.getBean("deployManager");
			jbpmContext = jbpmConfiguration.createJbpmContext();
			String config = MessageProperties.getConfigRootPath()
					+ "/config/system/jbpm/";
			java.io.File directory = new java.io.File(config);
			if (directory.isDirectory()) {
				String[] filelist = directory.list();
				for (int i = 0; i < filelist.length; i++) {
					String filename = config + filelist[i];
					File file = new File(filename);
					if (file.isFile()
							&& (file.getName().endsWith(".zip") || file
									.getName().endsWith(".par"))) {
						ZipInputStream zis = new ZipInputStream(
								new FileInputStream(file));
						ProcessDefinition processDefinition = ProcessDefinition
								.parseParZipInputStream(zis);
						if (jbpmContext.getGraphSession()
								.findLatestProcessDefinition(
										processDefinition.getName()) == null) {
							jbpmContext
									.deployProcessDefinition(processDefinition);
							DeployInstance deployInstance = new DeployInstance();
							deployInstance.setObjectId(UUID32.getUUID());
							deployInstance.setObjectName(processDefinition
									.getName());
							deployInstance.setProcessName(processDefinition
									.getName());
							deployInstance
									.setTitle(processDefinition.getName());
							deployInstance.setVersionNo(processDefinition
									.getVersion());
							deployInstance.setDeployDate(new java.util.Date());
							deployInstance.setProcessDefinitionId(String
									.valueOf(processDefinition.getId()));
							deployManager.saveDeployInstance(jbpmContext,
									deployInstance);
						}
					}
				}
			}
		} catch (Exception ex) {
			jbpmContext.setRollbackOnly();
			throw new RuntimeException("Load System ProcessDefinition error:"
					+ ex);
		} finally {
			Context.close(jbpmContext);
		}
	}
	%>
<%
 	    this.installSystemJbpmProcessDefinitions();
 %>